import { CircleShape } from "../../lib/kgame/d2/shape/CircleShape";
import CmpTrans from "../../lib/kgame/ecs/CmpTrans";
import { ECSCmp } from "../../lib/kgame/ecs/ECSCmp";
import ColliderFactory from "../factory/ColliderFactory";
import { CmpSnakeHead } from "./CmpSnakeHead";

/**
 * 蛇身组件
 */
export class CmpSnakeBody extends ECSCmp {

    /** 蛇头组件 */
    head: CmpSnakeHead;
    /** 碰撞器 */
    collider: CircleShape;
    /** 在蛇的第几节 */
    idx: number;
    /** 变换组件 */
    get trans() { return this.getCmp(CmpTrans); }

    /**
     * 初始化蛇身组件
     * @param head 蛇头组件
     * @param collider 碰撞器
     */
    onCreate(head: CmpSnakeHead) {
        this.head = head;
        this.idx = head.bodys.length;
        this.collider = ColliderFactory.createCircle(this.scene, 0);
        return this;
    }

    onClear(): void {
        this.head = null;
        this.collider.free();
        this.collider = null;
    }

}